$(function() {
    $(".tab-box .tab").on("click", function() {
        if(mainCtrl.curtabIdx == $(this).index()) {
            return;
        }
        mainCtrl.oldtabIdx = mainCtrl.curtabIdx;
        mainCtrl.curtabIdx = $(this).index();
        mainCtrl.clickTab = true;
        //$("#popup_confirm").show();
        //mainCtrl.confirmType = 1;
        $(this).addClass("sel").siblings(".tab").removeClass("sel");
        $(".cnt-box .cnt").eq($(this).index()).show().siblings(".cnt").hide();
        //切换tab时，需要检查是否存在templetId，存在了下次保存的时候就是调用update
        mainCtrl.curTempId = "QITA";
        for(var i = 0; i < mainCtrl.allSchemeTemplet.length; i++) {
            var scheme = mainCtrl.allSchemeTemplet[i];
            for(var j = 0; j < scheme.templetList.length; j++) {
                var temp = scheme.templetList[j];
                if(scheme.houseType == mainCtrl.layoutId && temp.templetType == mainCtrl.curtabIdx) {
                    mainCtrl.curTempId = temp.templetId;
                    break;
                }
            }
        }

        var buylist = mainCtrl.pageObj.tab1.buyList;
        var buylist2 = mainCtrl.pageObj.tab1.buyList2;
        if(mainCtrl.curtabIdx == 1) {
            buylist = mainCtrl.pageObj.tab2.buyList;
            buylist2 = mainCtrl.pageObj.tab2.buyList2;
        }
        if(mainCtrl.curtabIdx == 2) {
            buylist = mainCtrl.pageObj.tab3.buyList;
            buylist2 = mainCtrl.pageObj.tab3.buyList2;
        }
        if(buylist.length == 0 && buylist2[0].commList[0].commCode == "") {
            mainCtrl.queryTempletById();
        }

    });
    $("#popup_confirm .btn-close").on("click", function() {
        if(mainCtrl.confirmType == 1 && mainCtrl.clickTab) {
            mainCtrl.curtabIdx = mainCtrl.oldtabIdx;
        }
        if(mainCtrl.confirmType == 2 && mainCtrl.curtabIdx == 0) {
            mainCtrl.pageObj.tab1.buytype = mainCtrl.pageObj.tab1.selbuytype;
        }
        if(mainCtrl.confirmType == 3 && mainCtrl.curtabIdx == 0) {
            mainCtrl.pageObj.tab1.brandtype = mainCtrl.pageObj.tab1.selbrandtype;
        }
        $(this).parents(".u-popup").hide();
    });
    
    //主流程对象
    var mainCtrl = avalon.define({
        $id: "mainController",
        curtemp: 1,
        oldtabIdx: 0,
        curtabIdx: 0,
        curTempId: "QITA",
        clickTab: false,
        curshoppingCode: 0, //后台返回的当前tab的购买形式
        layoutList: [{
            id: "2-1",
            name: "2室1厅",
            templetList: []
        }, {
            id: "2-2",
            name: "2室2厅",
            templetList: []
        }, {
            id: "3-1",
            name: "3室1厅",
            templetList: []
        }, {
            id: "3-2",
            name: "3室2厅",
            templetList: []
        }, {
            id: "4-1",
            name: "4室1厅",
            templetList: []
        }, {
            id: "4-2",
            name: "4室2厅",
            templetList: []
        }],
        schemeTemplet: [], //存在的推荐方案
        allSchemeTemplet: [], 
        layoutName: "",
        layoutId: "",
        brandList: [],
        pageObj: {
            "tab1": {
                buytype: 0,
                selbuytype: 0,
                buyList: [], //单机对应的房间列表
                buyList2: [{
                    commList: [{
                        commCode: "",
                        commName: "",
                        price: "",
                        roomCode: "7",
                        roomName: "套机"
                    }],
                    positionCode: "3",
                    positionName: "套机"
                }], //.套机对应的房间列表
                brandtype: "",
                selbrandtype: "",
                remark: ""
            },
            "tab2": {
                buytype: 0,
                selbuytype: 0,
                buyList: [], //单机.对应的房间列表
                buyList2: [{
                    commList: [{
                        commCode: "",
                        commName: "",
                        price: "",
                        roomCode: "7",
                        roomName: "套机"
                    }],
                    positionCode: "3",
                    positionName: "套机"
                }], //.套机对应的房间列表
                brandtype: "",
                selbrandtype: "",
                remark: ""
            },
            "tab3": {
                buytype: 0,
                selbuytype: 0,
                buyList: [], //单机.对应的房间列表
                buyList2: [{
                    commList: [{
                        commCode: "",
                        commName: "",
                        price: "",
                        roomCode: "7",
                        roomName: "套机"
                    }],
                    positionCode: "3",
                    positionName: "套机"
                }], //.套机对应的房间列表
                brandtype: "",
                selbrandtype: "",
                remark: ""
            }
        },
        confirmType: 1,
        selBuyType: function(type) {
            $("#popup_confirm").show();
            mainCtrl.confirmType = 2;
        },
        selBrandType: function(type) {
            $("#popup_confirm").show();
            mainCtrl.confirmType = 3;
        },
        queryTempletById: function(type) {
            mainCtrl.curTempId = "QITA";
            for(var i = 0; i < mainCtrl.schemeTemplet.length; i++) {
                var temp = mainCtrl.schemeTemplet[i];
                if(temp.templetType == mainCtrl.curtabIdx) {
                    mainCtrl.curTempId = temp.templetId;
                    break;
                }
            }
            //          if(type == "QITA") {
            //              mainCtrl.curTempId = "QITA";
            //          }
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "houseType": mainCtrl.layoutId,
                    "templetId": type == "QITA" ? "QITA" : mainCtrl.curTempId
                },
                url: DI.airCondition_queryTempletById,
                success: function(response) {
                    var data = response.data;
                    $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).find(".note-len .u-textarea").val(data.remark ? data.remark : "");
                    mainCtrl.curshoppingCode = data.shoppingCode;

                    if(mainCtrl.curtabIdx == 0) {
                        if(data.shoppingCode == "1") {
                            mainCtrl.pageObj.tab1.buyList2 = [];
                            mainCtrl.pageObj.tab1.buyList2 = data.placeList;
                        } else {
                            mainCtrl.pageObj.tab1.buyList = [];
                            mainCtrl.pageObj.tab1.buyList = data.placeList;
                        }
                        //mainCtrl.pageObj.tab1.remark = data.remark ? data.remark : "";

                        //如果存在tempid，说明是更新接口
                        if(mainCtrl.curTempId != "QITA") {
                            mainCtrl.pageObj.tab1.buytype = data.shoppingCode;
                            mainCtrl.pageObj.tab1.selbuytype = data.shoppingCode;
                            mainCtrl.pageObj.tab1.brandtype = data.brandCode;
                            mainCtrl.pageObj.tab1.selbrandtype = data.brandCode;
                        } else {
                            mainCtrl.pageObj.tab1.buytype = 0;
                            mainCtrl.pageObj.tab1.selbuytype = 0;
                            mainCtrl.pageObj.tab1.brandtype = mainCtrl.brandList[0].brandCode;
                            mainCtrl.pageObj.tab1.selbrandtype = mainCtrl.brandList[0].brandCode;
                        }
                    }
                    if(mainCtrl.curtabIdx == 1) {
                        if(data.shoppingCode == "1") {
                            mainCtrl.pageObj.tab2.buyList2 = [];
                            mainCtrl.pageObj.tab2.buyList2 = data.placeList;
                        } else {
                            mainCtrl.pageObj.tab2.buyList = [];
                            mainCtrl.pageObj.tab2.buyList = data.placeList;
                        }
                        //mainCtrl.pageObj.tab2.remark = data.remark ? data.remark : "";
                        //如果存在tempid，说明是更新接口
                        if(mainCtrl.curTempId != "QITA") {
                            mainCtrl.pageObj.tab2.buytype = data.shoppingCode;
                            mainCtrl.pageObj.tab2.selbuytype = data.shoppingCode;
                            mainCtrl.pageObj.tab2.brandtype = data.brandCode;
                            mainCtrl.pageObj.tab2.selbrandtype = data.brandCode;
                        } else {
                            mainCtrl.pageObj.tab2.buytype = 0;
                            mainCtrl.pageObj.tab2.selbuytype = 0;
                            mainCtrl.pageObj.tab2.brandtype = mainCtrl.brandList[0].brandCode;
                            mainCtrl.pageObj.tab2.selbrandtype = mainCtrl.brandList[0].brandCode;
                        }
                    }
                    if(mainCtrl.curtabIdx == 2) {
                        if(data.shoppingCode == "1") {
                            mainCtrl.pageObj.tab3.buyList2 = [];
                            mainCtrl.pageObj.tab3.buyList2 = data.placeList;
                        } else {
                            mainCtrl.pageObj.tab3.buyList = [];
                            mainCtrl.pageObj.tab3.buyList = data.placeList;
                        }
                        //mainCtrl.pageObj.tab3.remark = data.remark ? data.remark : "";
                        //如果存在tempid，说明是更新接口
                        if(mainCtrl.curTempId != "QITA") {
                            mainCtrl.pageObj.tab3.buytype = data.shoppingCode;
                            mainCtrl.pageObj.tab3.selbuytype = data.shoppingCode;
                            mainCtrl.pageObj.tab3.brandtype = data.brandCode;
                            mainCtrl.pageObj.tab3.selbrandtype = data.brandCode;
                        } else {
                            mainCtrl.pageObj.tab3.buytype = 0;
                            mainCtrl.pageObj.tab3.selbuytype = 0;
                            mainCtrl.pageObj.tab3.brandtype = mainCtrl.brandList[0].brandCode;
                            mainCtrl.pageObj.tab3.selbrandtype = mainCtrl.brandList[0].brandCode;
                        }
                    }
                    //把列表保存起来， 在切换购买形式/空调品牌 时需要保留
                    //mainCtrl.saveOldList(data.placeList);

                },
                error: function() {

                }
            });
        },
        checkCommcode: function() {
            var isErr = false;
            var curplacelist = $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).find(".place");
            if(mainCtrl.curtabIdx == 0 && mainCtrl.pageObj.tab1.buytype == 1) {
                curplacelist = $(".cnt-box .cnt").eq(0).find(".place2");
            }
            if(mainCtrl.curtabIdx == 1 && mainCtrl.pageObj.tab2.buytype == 1) {
                curplacelist = $(".cnt-box .cnt").eq(1).find(".place2");
            }
            if(mainCtrl.curtabIdx == 2 && mainCtrl.pageObj.tab3.buytype == 1) {
                curplacelist = $(".cnt-box .cnt").eq(2).find(".place2");
            }
            for(var i = 0; i < curplacelist.length; i++) {
                var trlist = $(curplacelist[i]).find("tr");
                for(var j = 0; j < trlist.length; j++) {
                    if($(trlist[j]).find(".u-ipt").val() == "") {
                        isErr = true;
                        $(trlist[j]).find(".u-ipt").next(".u-error").show().text("请输入9位商品编码");
                    }
                }
            }
            var errorList = $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).find(".u-error");
            for(var i = 0; i < errorList.length; i++) {
                if($(errorList[i]).css("display") == "block") {
                    isErr = true;
                    break;
                }
            }
            return isErr;
        },
        getSaveParam: function() {
            var dataParam = {
                brandCode: "",
                brandName: $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).find(".brand option:selected").text(),
                houseType: mainCtrl.layoutId,
                houseTypeName: mainCtrl.layoutName,
                placeList: [{
                    commList: [{
                        commCode: "",
                        commName: "",
                        price: "",
                        roomCode: "",
                        roomName: ""
                    }],
                    positionCode: "",
                    positionName: ""
                }],
                remark: "",
                shoppingCode: "",
                templetName: "",
                templetType: mainCtrl.curtabIdx,
                totalPrice: "",
                size: ""
            };
            dataParam.remark = $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).find(".note-len .u-textarea").val();
            if(mainCtrl.curtabIdx == 0) {
                var placeList = mainCtrl.pageObj.tab1.buyList.$model;
                if(mainCtrl.pageObj.tab1.buytype == 1) {
                    placeList = mainCtrl.pageObj.tab1.buyList2.$model;
                }
                dataParam.brandCode = mainCtrl.pageObj.tab1.brandtype;
                dataParam.placeList = placeList;
                //dataParam.remark = mainCtrl.pageObj.tab1.remark;
                dataParam.shoppingCode = mainCtrl.pageObj.tab1.buytype;
                dataParam.templetName = "经济环保";
            } else if(mainCtrl.curtabIdx == 1) {
                var placeList = mainCtrl.pageObj.tab2.buyList.$model;
                if(mainCtrl.pageObj.tab2.buytype == 1) {
                    placeList = mainCtrl.pageObj.tab2.buyList2.$model;
                }
                dataParam.brandCode = mainCtrl.pageObj.tab2.brandtype;
                dataParam.placeList = placeList;
                //dataParam.remark = mainCtrl.pageObj.tab2.remark;
                dataParam.shoppingCode = mainCtrl.pageObj.tab2.buytype;
                dataParam.templetName = "中档宜居";
            } else {
                var placeList = mainCtrl.pageObj.tab3.buyList.$model;
                if(mainCtrl.pageObj.tab3.buytype == 1) {
                    placeList = mainCtrl.pageObj.tab3.buyList2.$model;
                }
                dataParam.brandCode = mainCtrl.pageObj.tab3.brandtype;
                dataParam.placeList = placeList;
                //dataParam.remark = mainCtrl.pageObj.tab3.remark;
                dataParam.shoppingCode = mainCtrl.pageObj.tab3.buytype;
                dataParam.templetName = "高档奢华";
            }

            return dataParam;
        },
        save: function() {
            //如果有商品编码为空，不操作 ; 如果商品编码查到后报错，不操作
            if(mainCtrl.checkCommcode()) {
                return;
            }
            //获取保存接口参数
            var dataParam = mainCtrl.getSaveParam();

            var saveURL = DI.airCondition_submitSchemeTemplet,
                isAdd = true;
            if(mainCtrl.curTempId != "QITA") {
                dataParam.templetId = mainCtrl.curTempId;
                saveURL = DI.airCondition_updateSchemeTemplet;
                isAdd = false;
            }

            dataParam = JSON.stringify(dataParam);
            $(".loadData").show();
            $.ajax({
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=UTF-8",
                data: dataParam,
                url: saveURL,
                success: function(response) {
                    if(response.retFlag == 1) {
                        PLATFORM.showTips("success", "保存成功");
                        //mainCtrl.queryTempletById(); //重新查一遍
                        mainCtrl.refreshData(1);
                    } else {
                        $(".loadData").hide();
                        PLATFORM.showTips("error", response.errorMessage);
                    }
                },
                error: function() {
                    $(".loadData").hide();
                }
            });

        },
        editPlan: function(row) {
            mainCtrl.layoutName = row.name;
            mainCtrl.layoutId = row.id;
            mainCtrl.schemeTemplet = row.templetList;
            mainCtrl.curtemp = 2;
            mainCtrl.curtabIdx = 0;
            //          mainCtrl.pageObj.tab1.buyList = [];
            //          mainCtrl.pageObj.tab1.buyList2 = [];
            //          mainCtrl.pageObj.tab2.buyList = [];
            //          mainCtrl.pageObj.tab2.buyList2 = [];
            //          mainCtrl.pageObj.tab3.buyList = [];
            //          mainCtrl.pageObj.tab3.buyList2 = [];
            mainCtrl.pageObj = {
                "tab1": {
                    buytype: 0,
                    selbuytype: 0,
                    buyList: [], //单机对应的房间列表
                    buyList2: [{
                        commList: [{
                            commCode: "",
                            commName: "",
                            price: "",
                            roomCode: "7",
                            roomName: "套机"
                        }],
                        positionCode: "3",
                        positionName: "套机"
                    }], //.套机对应的房间列表
                    brandtype: "",
                    selbrandtype: "",
                    remark: ""
                },
                "tab2": {
                    buytype: 0,
                    selbuytype: 0,
                    buyList: [], //单机.对应的房间列表
                    buyList2: [{
                        commList: [{
                            commCode: "",
                            commName: "",
                            price: "",
                            roomCode: "7",
                            roomName: "套机"
                        }],
                        positionCode: "3",
                        positionName: "套机"
                    }], //.套机对应的房间列表
                    brandtype: "",
                    selbrandtype: "",
                    remark: ""
                },
                "tab3": {
                    buytype: 0,
                    selbuytype: 0,
                    buyList: [], //单机.对应的房间列表
                    buyList2: [{
                        commList: [{
                            commCode: "",
                            commName: "",
                            price: "",
                            roomCode: "7",
                            roomName: "套机"
                        }],
                        positionCode: "3",
                        positionName: "套机"
                    }], //.套机对应的房间列表
                    brandtype: "",
                    selbrandtype: "",
                    remark: ""
                }
            };

            $(".tab-box .tab").eq(0).addClass("sel").siblings(".tab").removeClass("sel");
            $(".cnt-box .cnt").eq(0).show().siblings(".cnt").hide();
            //$(".tab-box .tab").eq(0).click();
            //查询品牌列表
            $.ajax({
                type: "post",
                dataType: "json",
                url: DI.airCondition_queryBrandList,
                success: function(response) {
                    var data = response.data;
                    mainCtrl.brandList = data;
                    //品牌有了之后才能去查套机信息
                    mainCtrl.queryTempletById();
                },
                error: function() {

                }
            });

        },
        returnTop: function() {
            $("#popup_confirm").show();
            mainCtrl.confirmType = 1;
            mainCtrl.clickTab = false;
        },
        clearCommCode: function() {
            //如果切换了购买形式或品牌，商品编码都清空
            var buyList = mainCtrl.pageObj.tab1.buyList;
            if(mainCtrl.curtabIdx == 0) {
                buyList = mainCtrl.pageObj.tab1.buyList;
            } else if(mainCtrl.curtabIdx == 1) {
                buyList = mainCtrl.pageObj.tab2.buyList;
            } else {
                buyList = mainCtrl.pageObj.tab3.buyList;
            }
            for(var i = 0; i < buyList.length; i++) {
                var commList = buyList[i].commList;
                for(var j = 0; j < commList.length; j++) {
                    commList[j].commCode = "";
                    commList[j].commName = "";
                }
            }
//          if(buyList.length > 0){
//              if(mainCtrl.curtabIdx == 0) {
//                  mainCtrl.pageObj.tab1.buyList = [];
//                  mainCtrl.pageObj.tab1.buyList = buyList;
//              } else if(mainCtrl.curtabIdx == 1) {
//                  mainCtrl.pageObj.tab2.buyList = [];
//                  mainCtrl.pageObj.tab2.buyList = buyList;
//              } else {
//                  mainCtrl.pageObj.tab3.buyList = [];
//                  mainCtrl.pageObj.tab3.buyList = buyList;
//              }
//          }
            $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).find(".comm-name").text("");
           
            //$(".comm-name").text("");

        },
        clearCommCode2: function() {
            var emptyList = [{
                commList: [{
                    commCode: "",
                    commName: "",
                    price: "",
                    roomCode: "7",
                    roomName: "套机"
                }],
                positionCode: "3",
                positionName: "套机"
            }];
            if(mainCtrl.curtabIdx == 0) {
                mainCtrl.pageObj.tab1.buyList2 = [];
                mainCtrl.pageObj.tab1.buyList2 = emptyList;
            } else if(mainCtrl.curtabIdx == 1) {
                mainCtrl.pageObj.tab2.buyList2 = [];
                mainCtrl.pageObj.tab2.buyList2 = emptyList;
            } else {
                mainCtrl.pageObj.tab3.buyList2 = [];
                mainCtrl.pageObj.tab3.buyList2 = emptyList;
            }

        },
        switchBuyType: function() {
            if(mainCtrl.curtabIdx == 0) {
                mainCtrl.pageObj.tab1.selbuytype = mainCtrl.pageObj.tab1.buytype;
                if(mainCtrl.pageObj.tab1.buytype == 0) {
                    var buyList = mainCtrl.pageObj.tab1.buyList;
                    //如果单独机型没有维护过，则重新查询
                    if(buyList.length == 0) {
                        mainCtrl.queryTempletById("QITA");
                    }
                } else {
                    var buyList = mainCtrl.pageObj.tab1.buyList2;
                    //如果套机没有维护过，则重新清空
                    if(buyList.length == 0 || buyList[0].commList[0].commCode == "") {
                        mainCtrl.pageObj.tab1.buyList2 = [{
                            commList: [{
                                commCode: "",
                                commName: "",
                                price: "",
                                roomCode: "7",
                                roomName: "套机"
                            }],
                            positionCode: "3",
                            positionName: "套机"
                        }];
                    }
                }
            }
            if(mainCtrl.curtabIdx == 1) {
                mainCtrl.pageObj.tab2.selbuytype = mainCtrl.pageObj.tab2.buytype;
                if(mainCtrl.pageObj.tab2.buytype == 0) {
                    var buyList = mainCtrl.pageObj.tab2.buyList;
                    //如果单独机型没有维护过，则重新查询
                    if(buyList.length == 0) {
                        mainCtrl.queryTempletById("QITA");
                    }
                } else {
                    var buyList = mainCtrl.pageObj.tab2.buyList2;
                    //如果套机没有维护过，则重新清空
                    if(buyList.length == 0 || buyList[0].commList[0].commCode == "") {
                        mainCtrl.pageObj.tab2.buyList2 = [{
                            commList: [{
                                commCode: "",
                                commName: "",
                                price: "",
                                roomCode: "7",
                                roomName: "套机"
                            }],
                            positionCode: "3",
                            positionName: "套机"
                        }];
                    }
                }
            }
            if(mainCtrl.curtabIdx == 2) {
                mainCtrl.pageObj.tab3.selbuytype = mainCtrl.pageObj.tab3.buytype;
                if(mainCtrl.pageObj.tab3.buytype == 0) {
                    var buyList = mainCtrl.pageObj.tab3.buyList;
                    //如果单独机型没有维护过，则重新查询
                    if(buyList.length == 0) {
                        mainCtrl.queryTempletById("QITA");
                    }
                } else {
                    var buyList = mainCtrl.pageObj.tab3.buyList2;
                    //如果套机没有维护过，则重新清空
                    if(buyList.length == 0 || buyList[0].commList[0].commCode == "") {
                        mainCtrl.pageObj.tab3.buyList2 = [{
                            commList: [{
                                commCode: "",
                                commName: "",
                                price: "",
                                roomCode: "7",
                                roomName: "套机"
                            }],
                            positionCode: "3",
                            positionName: "套机"
                        }];
                    }
                }
            }
        },
        confirmSure: function() {
            if(mainCtrl.confirmType == 1) {
                if(mainCtrl.clickTab) {
                    $(".tab-box .tab").eq(mainCtrl.curtabIdx).addClass("sel").siblings(".tab").removeClass("sel");
                    $(".cnt-box .cnt").eq(mainCtrl.curtabIdx).show().siblings(".cnt").hide();

                    mainCtrl.queryTempletById();

                } else {
                    mainCtrl.curtemp = 1;
                }

            } else if(mainCtrl.confirmType == 2) {
                mainCtrl.switchBuyType();
                mainCtrl.clearCommCode();
                mainCtrl.clearCommCode2();
            } else {
                if(mainCtrl.curtabIdx == 0) {
                    mainCtrl.pageObj.tab1.selbrandtype = mainCtrl.pageObj.tab1.brandtype;
                } else if(mainCtrl.curtabIdx == 1) {
                    mainCtrl.pageObj.tab2.selbrandtype = mainCtrl.pageObj.tab2.brandtype;
                } else {
                    mainCtrl.pageObj.tab3.selbrandtype = mainCtrl.pageObj.tab3.brandtype;
                }
                mainCtrl.clearCommCode();
                mainCtrl.clearCommCode2();
            }
            $("#popup_confirm").hide();
        },
        searchCode: function() {
            if($(this).val().length != 9) {
                $(this).next(".u-error").show().text("请输入9位商品编码");
                //$(this).focus();
            } else {
                $(this).next(".u-error").hide();
                var _self = $(this);
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: {
                        commCode: _self.val()
                    },
                    url: DI.airCondition_queryProductDb,
                    success: function(response) {
                        if(response.retFlag == 1) {
                            var data = response.data;
                            //通码商品
                            if(data.itemType == "01") {
                                _self.next(".u-error").show().text("不能配置通码商品");
                            } else {
                                _self.prev("input").val(data.itemName);
                                _self.parents("td").next("td").text(data.itemName);
                            }
                        } else {
                            _self.next(".u-error").show().text("商品编码查询失败");
                            _self.prev("input").val("");
                            _self.parents("td").next("td").text("");
                        }
                    },
                    error: function() {

                    }
                });
            }
        },
        refreshData: function(type) {
            //查询推荐方案
            $.ajax({
                type: "post",
                dataType: "json",
                url: DI.airCondition_querySchemeTemplet,
                success: function(response) {
                    var data = response.data;
                    //mainCtrl.schemeTemplet = data;
                    mainCtrl.allSchemeTemplet = data;
                    
                    for(var i = 0; i < data.length; i++) {
                        var house = data[i];
                        for(var j = 0; j < mainCtrl.layoutList.length; j++) {
                            var temp = mainCtrl.layoutList[j];
                            if(house.houseType == temp.id) {
                                mainCtrl.layoutList[j].templetList = house.templetList;
                                break;
                            }
                        }
                    }
                    if(type == 1) {
                        for(var i = 0; i < mainCtrl.allSchemeTemplet.length; i++) {
                            var scheme = mainCtrl.allSchemeTemplet[i];
                            for(var j = 0; j < scheme.templetList.length; j++) {
                                var temp = scheme.templetList[j];
                                if(scheme.houseType == mainCtrl.layoutId && temp.templetType == mainCtrl.curtabIdx) {
                                    mainCtrl.curTempId = temp.templetId;
                                    break;
                                }
                            }
                        }
                        //保存成功
                        $(".loadData").hide();
                    }
                },
                error: function() {

                }
            });
        }
    });

    mainCtrl.refreshData();

    avalon.scan();
});